<?php
/**
 * Created by PhpStorm.
 * User: micoya
 * Date: 2018/11/29
 * Time: 2:21 PM
 */

namespace app\index\controller;

use app\common\base\Form;
use think\Controller;
use think\Db;
use think\Session;


class ServiceController extends Controller {

    public function Add() {

        if (!Session::has('user')) {
            // return Error('请登录');
        }

        Form::Validate([
            'type'                   =>  'require',
            'content'                =>  'require',
            'address_full'           =>  'require',
            'phone'                  =>  'require',
            'user_name'              =>  'require',

        ]);

        $data = [
            // fix
            // trade
            'type'               =>  input('type'),
            'content'            =>  input('content'),
            'address_full'       =>  input('address_full'),
            'phone'              =>  input('phone'),
            'user_name'          =>  input('user_name'),
            'user_id'            =>  Session::has('user') ? Session::get('user')['id'] : 0,
        ];

        $result = Db::table('service')->insert($data);

        if (!$result) {
            return Error('提交失败');
        }

        return Success('提交成功');

    }

    public function GetList() {

        if (!Session::has('user')) {
            return Error('请登录');
        }

        $user_id = session('user')['id'];

        $result = Db::table('service')->where('user_id', $user_id)->select();

        return Success('获取成功', $result);
    }


    public function GetOne() {

        if (!Session::has('user')) {
            // return Error('请登录');
        }

        Form::Validate([
            'id'    =>  'require|integer'
        ]);

        $user_id = session('user')['id'];

        $result = Db::table('service')->where('user_id', $user_id)->where('id', input('id'))->find();
        if (!$result) {
            return Error('获取失败');
        }

        return Success('获取成功', $result);

    }

}